# web/routes.py
import json

from flask import Blueprint, render_template, request

from data.mysql_db import DataManager

data_manager = DataManager()

# 创建蓝图
web_bp = Blueprint('web', __name__,
                   template_folder='templates',
                   static_folder='static')


@web_bp.route('/')
def index():
    """系统首页"""
    return render_template('index.html')


# 数据管理模块
@web_bp.route('/data')
def data_management():
    """数据管理页面"""
    return render_template('data_management.html')


@web_bp.route('/posts')
def posts():
    page = request.args.get('page', 1, type=int)
    per_page = 20
    result = data_manager.get_posts(page=page, per_page=per_page)

    return render_template(
        'posts.html',
        posts=result['items'],
        page=result['page'],
        per_page=per_page,
        total=result['total']
    )


# 策略管理模块
# @web_bp.route('/strategies')
# def strategies():
#     """策略管理页面"""
#     strategy_list = get_strategy_list()  # 获取策略列表
#     return render_template('strategies.html', strategies=strategy_list)

# 回测模块
@web_bp.route('/backtest')
def backtest():
    """回测页面"""
    return render_template('backtest.html')


# 模拟交易模块
@web_bp.route('/simulator')
def simulator():
    """模拟交易页面"""
    return render_template('simulator.html')


# 风险管理模块
@web_bp.route('/risk')
def risk():
    """风险管理页面"""
    return render_template('risk.html')


# 预警通知模块
@web_bp.route('/notifications')
def notifications():
    """预警通知页面"""
    return render_template('notifications.html')


# 股票池管理模块
@web_bp.route('/stock_pool')
def stock_pool():
    """股票池管理页面"""
    return render_template('stock_pool.html')


# 页面错误处理
@web_bp.errorhandler(404)
def page_not_found(e):
    return render_template('404.html'), 404


# 初始化时注册蓝图
def init_routes(app):
    app.register_blueprint(web_bp)
